package com.yonyou.jrmob.contacts.contact.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.contact.activity.ContactsDetailActivity_ct;
import com.yonyou.jrmob.contacts.contact.activity.InterfacePeopleActivity_ct;
import com.yonyou.jrmob.contacts.contact.activity.MyDepartmentActivity_ct;
import com.yonyou.jrmob.contacts.contact.activity.SearchContactActivity_ct;
import com.yonyou.jrmob.contacts.contact.activity.UsefulTelephoneActivity_ct;
import com.yonyou.jrmob.contacts.contact.adapter.SortAdapter_ct;
import com.yonyou.jrmob.contacts.contact.bean.ContactsVO_ct;
import com.yonyou.jrmob.contacts.contact.dbhelper.DBManager_ct;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.bingoogolapple.titlebar.BGATitleBar;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.UiUtils;
import jrmob.yonyou.com.commonlib.baseui.fragment.BaseFragmentMob_lib;
import jrmob.yonyou.com.commonlib.view.auto.AutoCompleteEditView;

public class ContactsMainFragment_ct extends BaseFragmentMob_lib {

    private SwipeMenuListView listView;
    private List<ContactsVO_ct> recentlyContacts = new ArrayList<ContactsVO_ct>();
    private SortAdapter_ct recentlyContactAdapter;
    private AutoCompleteEditView searchView;
    private LinearLayout searchClick;
    private LinearLayout myDepartment;
    private LinearLayout myAttention;
    private LinearLayout userPhone;
    private LinearLayout iterfacePeople;
    private String opFlag;//我的关注标识
    private DBManager_ct dbmr;
    private BGATitleBar titleBar;
    private View statusBar;
    @Override
    public View initViews() {

        View view = View.inflate(mActivity, R.layout.fragment_main_contacts_ct,
                null);
        dbmr = new DBManager_ct(mActivity);
        listView = (SwipeMenuListView) view.findViewById(R.id.recently_contact_list);
        searchView = (AutoCompleteEditView) view
                .findViewById(R.id.search_view_pledge);
        searchView.getTv().setVisibility(View.GONE);
        // 默认删除按钮不显示
        searchView.getIv().setVisibility(View.INVISIBLE);
        searchClick = (LinearLayout) view.findViewById(R.id.search_btn);
        myDepartment = (LinearLayout) view.findViewById(R.id.my_department_ll);
        myAttention = (LinearLayout) view.findViewById(R.id.my_attention_ll);
        userPhone = (LinearLayout) view.findViewById(R.id.use_phone_ll);
        iterfacePeople = (LinearLayout) view.findViewById(R.id.iterface_people_ll);
        titleBar = (BGATitleBar) view.findViewById(R.id.normal_title_bar);
        statusBar = view.findViewById(R.id.normal_title_bar_status_bar);
        initSearch();
        initData();
        initOnClick();
        initTitleBar();
        return view;
    }
    private void initTitleBar(){
        titleBar.setTitleText("联系人");
        //statusBar.setBackgroundColor(getResources().getColor(R.color.white));
        //LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) statusBar.getLayoutParams();
        //lp.height = UiUtils.getStatusBarHeight();
        //statusBar.setLayoutParams(lp);
    }
    @Override
    public void initData() {
        recentlyContacts = dbmr.queryRecentlyContacts();
        setSlideListView();
        recentlyContactAdapter = new SortAdapter_ct(mActivity, recentlyContacts, false);
        listView.setAdapter(recentlyContactAdapter);
    }

    private void initSearch() {
        // 默认删除按钮不显示
        //searchView.getIv().setVisibility(View.INVISIBLE);
    }

    public void initOnClick() {
        searchClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent5 = new Intent();
                intent5.setClass(mActivity.getApplicationContext(), SearchContactActivity_ct.class);
                startActivity(intent5);
            }
        });
        myDepartment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.putExtra("opflag", "1");
                intent1.putExtra("title", "我的部门");
                intent1.setClass(mActivity.getApplicationContext(), MyDepartmentActivity_ct.class);
                startActivity(intent1);
            }
        });
        myAttention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent();
                intent2.putExtra("opflag", "0");
                intent2.putExtra("title", "我的关注");
                intent2.setClass(mActivity.getApplicationContext(), MyDepartmentActivity_ct.class);
                startActivity(intent2);
            }
        });
        userPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent3 = new Intent();
                intent3.putExtra("title", "常用电话");
                intent3.setClass(mActivity.getApplicationContext(), UsefulTelephoneActivity_ct.class);
                //intent3.setClass(mActivity.getApplicationContext(), ScheduleActivity.class);
                startActivity(intent3);
            }
        });
        iterfacePeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent4 = new Intent();
                //intent4.putExtra("title", "接口人");
                intent4.setClass(mActivity.getApplicationContext(), InterfacePeopleActivity_ct.class);
                startActivity(intent4);
            }
        });
        listView.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                listView.getParent().requestDisallowInterceptTouchEvent(false);
                v.onTouchEvent(event);
                return true;
            }
        });

    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initData();

    }

    /**
     * @desc 侧滑listview的监听与创建menu
     * @author lizhiqiang
     */
    public void setSlideListView() {

        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {

                SwipeMenuItem item_delete = new SwipeMenuItem(mActivity.getApplicationContext());
                item_delete.setWidth(dp2px(60));
                item_delete.setTitle("删除");
                item_delete.setTitleSize(15);
                item_delete.setTitleColor(Color.WHITE);
                item_delete.setBackground(R.drawable.menu_delete_selector_ct);
                menu.addMenuItem(item_delete);

            }
        };
        // set creator
        listView.setMenuCreator(creator);
        listView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
        //行点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {
                String name = recentlyContactAdapter.getItem(position).getContactName();
                Intent intent = new Intent();
                intent.putExtra("name", name);
                intent.putExtra("cuserId", recentlyContactAdapter.getItem(position)
                        .getCuserId());
                intent.setClass(mActivity.getApplicationContext(),
                        ContactsDetailActivity_ct.class);
                startActivity(intent);

            }
        });

        // 设置侧滑隐藏点击事件
        listView
                .setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(int position,
                                                   SwipeMenu menu, int index) {
                        switch (index) {
                            case 0:// 更多,弹出popwindow
                              /*  //showPopUpWindow(getCurrentFocus(), position);
                                break;
                            case 1:// 删除*/
                                String cuserid = recentlyContactAdapter.getItem(position).getCuserId();
                                recentlyContacts = dbmr.deleteRecentlyContact(cuserid);
                                //recentlyContactAdapter.updateListView(recentlyContacts);
                                updateDatas();
                               // Constant.isAllowSlide = true;
                                break;
                        }
                        return false;
                    }
                });



        listView.setOnMenuStateChangeListener(new SwipeMenuListView.OnMenuStateChangeListener() {
            @Override
            public void onMenuOpen(int i) {
               // Constant.isAllowSlide = false;
            }

            @Override
            public void onMenuClose(int i) {
                TimerTask task = new TimerTask(){
                    public void run(){
                       // Constant.isAllowSlide = true;
                    }
                };
                Timer timer = new Timer();
                timer.schedule(task,300);
            }
        });

    }

    void updateDatas() {
        recentlyContacts = dbmr.queryRecentlyContacts();
        recentlyContactAdapter = new SortAdapter_ct(
                mActivity, recentlyContacts, false);
        listView.setAdapter(recentlyContactAdapter);
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    private class ContactOnTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return true;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        listView.smoothCloseMenu();
    }
}